package com.shuyu.flutterplugintesttexture

import android.graphics.Color
import android.graphics.Rect
import android.util.Log
import android.view.Surface
import androidx.annotation.NonNull
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.embedding.engine.renderer.FlutterRenderer
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result
import io.flutter.plugin.common.PluginRegistry.Registrar
import io.flutter.view.TextureRegistry.SurfaceTextureEntry
import java.util.*
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit

/** FlutterplugintesttexturePlugin */
public class FlutterplugintesttexturePlugin : FlutterPlugin, MethodCallHandler {


    var renderer: FlutterRenderer? = null

    override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
        renderer = flutterPluginBinding.flutterEngine.renderer
        val channel = MethodChannel(flutterPluginBinding.getFlutterEngine().getDartExecutor(), "flutterplugintesttexture")
        channel.setMethodCallHandler(this);
    }

    // This static function is optional and equivalent to onAttachedToEngine. It supports the old
    // pre-Flutter-1.12 Android projects. You are encouraged to continue supporting
    // plugin registration via this function while apps migrate to use the new Android APIs
    // post-flutter-1.12 via https://flutter.dev/go/android-project-migration.
    //
    // It is encouraged to share logic between onAttachedToEngine and registerWith to keep
    // them functionally equivalent. Only one of onAttachedToEngine or registerWith will be called
    // depending on the user's project. onAttachedToEngine or registerWith must both be defined
    // in the same class.
    companion object {
        @JvmStatic
        fun registerWith(registrar: Registrar) {
            val channel = MethodChannel(registrar.messenger(), "flutterplugintesttexture")
            channel.setMethodCallHandler(FlutterplugintesttexturePlugin())
        }
    }

    val scheduledThreadPool = Executors.newScheduledThreadPool(2)

    override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
        if (call.method == "getPlatformVersion") {
            //result.success("Android ${android.os.Build.VERSION.RELEASE}")
            val handle: SurfaceTextureEntry = renderer!!.createSurfaceTexture()
            val surface = Surface(handle.surfaceTexture())
            //val map = HashMap<String, Long>()
            //map["textureId"] = handle.id()

            val task2 = object : TimerTask() {
                var i = 0
                override fun run() {
                    val canvas = surface.lockCanvas(Rect(0, 0, 300, 300))
                    val color = when {
                        i % 2 == 0 -> {
                            Color.BLUE;
                        }
                        i % 3 == 0 -> {
                            Color.RED
                        }
                        else -> {
                            Color.GREEN
                        }
                    }
                    canvas.drawColor(color)
                    surface.unlockCanvasAndPost(canvas)
                    i++
                }
            }
            scheduledThreadPool.scheduleWithFixedDelay(task2, 500, 500, TimeUnit.MILLISECONDS)
            result.success(handle.id())
        } else {
            result.notImplemented()
        }

    }

    override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
    }
}
